﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils.Menu;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_PERMISSION.Base.Objects;
using ERP_RORZE_PERMISSION.Forms;

namespace ERP_RORZE_PERMISSION.UserControls
{
    public partial class ucLogs : DevExpress.XtraEditors.XtraUserControl
    {
        private int index = 0;
        public int _action = 1;
        DXPopupMenu menu;

        public ucLogs()
        {
            InitializeComponent();
            InitDateRangeMenu();
            this.dropbtExport.DropDownControl = menu;
        }

        private void ucUser_Load(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            View();
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {

        }

        private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
        {

            if (gridView1.RowCount != 0)
                return;

            StringFormat drawFormat = new StringFormat();

            drawFormat.Alignment = drawFormat.LineAlignment = StringAlignment.Center;

            e.Graphics.DrawString("No items exist in this view", e.Appearance.Font, SystemBrushes.ControlDark, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), drawFormat);

        }

        #region Private methods

        public void BindDataGrid()
        {
            gridControl1.DataSource = BaseDAO.ExecuteQuery("select * from [222_Log] where convert(date,[222_Log].[Time]) = '" + DateTime.Now.ToShortDateString() + "' order by [Time] desc");
            gridView1.BestFitColumns();
            Utilities.GridUtil.SelectRow(gridView1, index, _action);
        }

        private void View()
        {
            Cursor.Current = Cursors.WaitCursor;
            gridControl1.DataSource = BaseDAO.ExecuteQuery("select * from [222_Log] where convert(date,[222_Log].[Time]) between '" + dateFrom.Value.Date.ToString("MM/dd/yyyy") + "' and '" + dateTo.Value.Date.ToString("MM/dd/yyyy") + "' order by [Time] desc");
            gridView1.BestFitColumns();
            Utilities.GridUtil.SelectRow(gridView1, index, _action);
            Cursor.Current = Cursors.Default;

        }

        protected virtual void InitDateRangeMenu()
        {
            menu = new DXPopupMenu();
            menu.Items.Add(new DXMenuItem(@"Expport selected rows", new EventHandler(OnExportRows), null, null, null, null));
            menu.Items.Add(new DXMenuItem(@"Expport all", new EventHandler(OnExportAll), null, null, null, null));

        }

        void OnExportRows(object sender, EventArgs e)
        {
            Utilities.ExportToExcel(this.gridView1, 0, true);
        }

        void OnExportAll(object sender, EventArgs e)
        {
            Utilities.ExportToExcel(this.gridView1, 1, true);

        }
        #endregion



    }
}
